package com.shhic.hhplt.server.biz;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import org.springframework.stereotype.Component;

import com.shhic.hhplt.server.util.FtpUtil;

@Component
public class FtpHelper implements IFtpHelper{
	private FtpUtil ftpUtil;
	
	@Override
	public void openFtp(String ftpUrl, String username, String password) {
		ftpUtil = new FtpUtil();
		URI ftpUri;
		try {
			ftpUri = new URI(ftpUrl);
			String path = ftpUri.getPath().equals("") ? "/" : ftpUri.getPath();
			int port = ftpUri.getPort() == -1 ? 21:ftpUri.getPort();
			ftpUtil.connect(path,ftpUri.getHost(),port,username,password);
		} catch (URISyntaxException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public void uploadFolder(String toSendDirName) {
		try{
			ftpUtil.upload(new File(toSendDirName));
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}

	@Override
	public void close() {
		ftpUtil.closeServer();
		ftpUtil = null;
	}

	
	
	
}
